package net.maku.jw.controller;

import com.alibaba.excel.util.StringUtils;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.jw.entity.Student;
import net.maku.jw.entity.SysMember;
import net.maku.jw.service.SysMemberService;
import net.maku.jw.util.HtmlunitUtil;
import org.apache.ibatis.jdbc.Null;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import net.maku.framework.common.utils.Result;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.annotation.Resource;

/**
 * 小程用户信息(SysMember)控制层
 * 绑定教务    课表不进行存储，但会建一个Bean进行拉取解析
 * @author huagnjunhao
 * @since 2024-04-05 14:15:34
 */
@RestController
@RequestMapping("/jw")
@Tag(name = "会员管理")
@AllArgsConstructor
public class SysMemberController {
    //开学日期 2022/08/29


    /**
     * 服务对象
     */
    @Resource
    private SysMemberService sysMemberService;


    @Tag(name = "vx登录")
    @PostMapping(value = "login")
    public Result<SysMember> login(@RequestBody @Validated SysMember sysMember) throws Exception {
        // 服务器重启的话，静态对象消失，得重新绑定，，可以redis缓存，但教务系统的token有效期不到一天，没必要，，注意有token不需要登录
        Result<SysMember> result = sysMemberService.bingjw(sysMember);
        return result;
    }

    @PostMapping("/getstudentinfo")
    @Tag(name = "获取学生信息")
    public Result<Student> getStudentInfo(@RequestBody SysMember member) throws Exception {
        SysMember studentInfo = HtmlunitUtil.getStudentInfo(member);
        if (studentInfo == null) {
            return Result.error("获取学生信息失败");
        }
        return Result.ok(studentInfo.getStudent());
    }

//    @PostMapping("/getstugrade")
//    @Tag(name = "获取学生成绩")
//    public GradeVo getStudentGrade(@RequestBody GetGradeInfo getGradeInfo) {
//        return studentService.getStudentGrade(getGradeInfo);
//    }





    //-----时间充足再开发--------------------
//    @PostMapping("/getstuhavestudied")
//    @ApiOperation("获取已修各模块的课程学分情况")
//    public DataVo getStudentHaveStudied(@RequestBody GetHaveStudiedInfo getHaveStudiedInfo)
//    @GetMapping("getstuquadeve/{studentId}")
//    @ApiOperation("获取素质拓展已修情况")
//    public DataVo getStuQuaDeve(@PathVariable("studentId") String studentId)
//    @PostMapping("getexamtime")
//    @ApiOperation("获取学生的考试时间")
//    public DataVo getExamTime(@RequestBody GetExamTimeInfo getExamTimeInfo)
    //--------------------------


    /**
     * 通过主键查询单条数据
     * @param sysMember 参数对象
     * @return 单条数据
     */
    @GetMapping(value = "getMember")
    @Tag(name = "获取学生成绩")
    public Result<SysMember> selectOne(SysMember sysMember) {
        SysMember result = sysMemberService.selectById(sysMember.getId());
        if (result != null) {
            return Result.ok(result);
        }
        return Result.error("查询失败");
    }

    /**
     * 新增一条数据
     *
     * @param sysMember 实体类
     * @return Result对象
     */
    @PostMapping(value = "insert")
    @Tag(name = "获取学生成绩")
    public Result<SysMember> insert(@RequestBody SysMember sysMember) {
        //教务登录
        int result = sysMemberService.insert(sysMember);
        if (result > 0) {
            sysMember.setId(sysMemberService.selectLastId());
            return Result.ok(sysMember);
        }
        return Result.error("新增失败");
    }

    /**
     * 修改一条数据
     *
     * @param sysMember 实体类
     * @return Result对象
     */
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    @Tag(name = "获取学生成绩")
    public Result<SysMember> update(@RequestBody SysMember sysMember) {
        SysMember result = sysMemberService.update(sysMember);
        if (result != null) {
            return Result.ok(result);
        }
        return Result.error("修改失败");
    }

    /**\
     *  解除绑定
     */
    @PostMapping(value = "delBing")
    @Tag(name = "获取学生成绩")
    public Result<SysMember> updateBing(@RequestBody SysMember sysMember) {
        sysMember.setIsBind(0);
        // 删除cache
        SysMember result = sysMemberService.update(sysMember);
        if (result != null) {
            return Result.ok(result);
        }
        return Result.error("解除绑定失败");
    }

    /**
     * 判定是否绑定
     *
     * @param id 参数对象
     * @return Result对象
     */
    @RequestMapping(value = "checkBing", method = RequestMethod.DELETE)
    @Tag(name = "获取学生成绩")
    public Result<Integer> checkBing(Integer id) {
        int result = sysMemberService.selecBingtById(Long.valueOf(id));
        if (result > 0) {
            return Result.ok(result);
        }
        return Result.error("获取绑定信息失败");
    }


    /**
     * 查询全部
     *
     * @return Result对象
     */
    @GetMapping(value = "selectAll")
    @Tag(name = "获取学生成绩")
    public Result<List<SysMember>> selectAll() {
        List<SysMember> sysMembers = sysMemberService.selectAll();
        if (sysMembers != null) {
            return Result.ok(sysMembers);
        }
        return Result.error("查询失败");
    }

    /**
     * 分页查询
     *
     * @param start 偏移
     * @param limit 条数
     * @return Result对象
     */
    @GetMapping(value = "selectPage")
    @Tag(name = "分页查询会员表")
    public Result<List> selectPage(Integer start, Integer limit) {
        start = (start - 1) * limit;
        List<SysMember> sysMembers = sysMemberService.selectPage(start, limit);
        if (sysMembers != null) {
            return Result.ok((List) sysMembers);
        }
        return Result.error("查询失败");
    }

    @PostMapping(value = "selectList")
    @Tag(name = "获取所有会员信息")
    public Result<List> selectPage(@RequestBody Map<String, Object> data) {
        System.out.println("data=" + data);


        SysMember sysMember = null;
        if (data.get("sysMember") != null) {
            // 将数据转成json字符串
            String jsonObject = JSON.toJSONString(data.get("sysMember"));
            //将json转成需要的对象
            sysMember = JSONObject.parseObject(jsonObject, SysMember.class);
        }

        Integer start = null;
        Integer limit = null;
        if (data.get("start") != null && data.get("limit") != null) {
            start = (Integer) data.get("start");
            limit = (Integer) data.get("limit");
            start = (start - 1) * limit;
        }
        List<SysMember> sysMembers = sysMemberService.selectList(sysMember, start, limit);
        if (sysMembers != null) {
            return Result.ok((List) sysMembers);
        }
        return Result.error("查询失败");
    }
}




